package nn.api.shiro;

import nn.db.entity.DtsAdmin;
import nn.db.service.AdminService;
import nn.db.service.PermissionService;
import nn.db.service.RoleService;
import nn.db.entity.DtsAdmin;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;



public class AdminAuthorizingRealm extends AuthorizingRealm {

//	private static final Logger logger = LoggerFactory.getLogger(AdminAuthorizingRealm.class);
	@Autowired
	private AdminService adminService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		if (principals == null) {
			throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
		}



		DtsAdmin admin = (DtsAdmin) getAvailablePrincipal(principals);
		Integer[] roleIds = admin.getRoleIds();
		String[] permissionStrs = permissionService.getPermissionStrs(roleIds);
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.setRoles(roleService.getRoleName(roleIds));
		info.setStringPermissions(new HashSet<>(Arrays.asList(permissionStrs)));
		return info;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		String username = upToken.getUsername();
		String password = new String(upToken.getPassword());
		System.out.println(username);
		System.out.println(password);
		if (StringUtils.isEmpty(username)) {
			throw new AccountException("用户名不能为空");
		}
		if (StringUtils.isEmpty(password)) {
			throw new AccountException("密码不能为空");
		}

		List<DtsAdmin> adminList = adminService.findAdmin(username);
		System.out.println(adminList.get(0).getPassword());
		Assert.state(adminList.size() < 2, "同一个用户名存在两个账户");
		if (adminList.size() == 0) {
			//logger.error("找不到用户（" + username + "）的帐号信息");
			throw new UnknownAccountException("找不到用户（" + username + "）的帐号信息");
		}
		DtsAdmin dtsAdmin = adminList.get(0);

//		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//		if (!encoder.matches(password, dtsAdmin.getPassword())) {
//			logger.error("找不到用户（" + username + "）的帐号信息");
//			throw new UnknownAccountException("找不到用户（" + username + "）的帐号信息");
//		}
		if (!password.equals(dtsAdmin.getPassword())) {
			//logger.error("找不到用户（" + username + "）的帐号信息");
			throw new UnknownAccountException("找不到用户（" + username + "）的帐号信息");
		}
		//return new SimpleAuthenticationInfo("", "", getName());
		return new SimpleAuthenticationInfo(dtsAdmin, password, getName());
	}

}
